package com.jb.repair.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.jb.repair.F1Application;
import com.jb.repair.common.AppConfig;
import com.jb.repair.model.DocH1Model;
import com.jb.repair.model.DocH2Model;
import com.jb.repair.model.DocModel;
import com.jb.repair.model.DzjlModel;
import com.jb.repair.model.FileLbModel;
import com.jb.repair.model.FssbModel;
import com.jb.repair.model.JxjlModel;
import com.jb.repair.model.LedgerModel;
import com.jb.repair.model.QxjlModel;
import com.jb.repair.model.SbbdzdyModel;
import com.jb.repair.model.SbbjModel;
import com.jb.repair.model.SbcjModel;
import com.jb.repair.model.SbsModel;
import com.jb.repair.model.SbzlModel;
import com.jb.repair.model.TtjlModel;
import com.jb.repair.model.YcjlModel;
import com.jb.repair.model.YdjlModel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by gaobin on 2015/11/3.
 */
public class Db {
    public static final String DB_NAME = "SCGLXT.db";

    //11.4 张进
    private static final String SBBJ_TABLE_NAME = "TB_EQ_SPAREPARTS";   //设备备件表名
    private static final String SBZL_TABLE_NAME = "TB_EQ_FILES";   //设备资料表名
    private static final String YCGL_TABLE_NAME = "TB_EQ_ABNORMAL";   //异常管理表名
    private static final String QXGL_TABLE_NAME = "TB_EQ_DEFECT";   //缺陷管理表名
    private static final String JXJL_TABLE_NAME = "TB_EQ_MAINTENANCE";   //检修记录表名
    private static final String DZJL_TABLE_NAME = "TB_EQ_RELAYACTION";   //动作记录表名
    private static final String TTJL_TABLE_NAME = "TB_EQ_TUOTUI";   //投退记录表名
    private static final String YDJL_TABLE_NAME = "TB_EQ_YIDONG";   //异动记录表名

    private static final String SBTZ_TABLE_NAME = "TB_EQ_NAV";   //设备台账表名
    private static final String JIBENXINXI_NAME = "TB_EQ_BASICINFO";//基本信息表
    private static final String FSSB_TABLE_NAME = "TB_EQ_ATTACHEQ";   //附属设备表名
    private static final String TASK_TABLE_NAME = "TB_SI_TASK";    //点检任务表名
    private static final String TASK_RESULT_STATISTICAL_TABLE_NAME = "TB_SI_TASK_RESULT_STATISTICAL";  //点检结果统计表名
    private static final String TASK_RESULT_TABLE_NAME = "TB_SI_TASK_RESULT";    //点检任务结果表表名
    private static final String DEV_INS_POSITION_TABLE_NAME = "TB_DEV_INS_POSITION";   //安装位置
    private static final String SI_PLAN_PLACE_TABLE_NAME = "TB_SI_PLAN_PLACE";   //点检计划位置
    private static final String SI_PLAN_DEV_DETAIL_TABLE_NAME = "TB_SI_PLAN_DEV_DETAIL";  //点检计划设备详情
    private static final String SI_PLAN_TABLE_NAME = "TB_SI_PLAN";   //点检计划
    private static final String SI_PLAN_PLACE_DETAIL_TABLE_NAME = "TB_SI_PLAN_PLACE_DETAIL"; // 点检计划位置详情
    private static final String PERSON_TABLE_NAME = "TB_SYS_PERSON"; // 人员表

    private static String TAG = "db";
    private static SQLiteDatabase db;
    private static Db mydb;
    String ssdc;

    private Db(Context context) {
        //防止内存泄露
        db = context.getApplicationContext().openOrCreateDatabase(AppConfig.DB_PATh, Context.MODE_PRIVATE, null);
        //本地获取电厂id
        ssdc = "C6EFDBD8-CF40-4727-B0DD-49C92E393305-00121";
    }

    public static synchronized Db getInstance(Context context) {
        if (mydb == null)
            mydb = new Db(context);
        return mydb;
    }

    public String getName1(String aim, String table, String key, String value) {
        Log.i("sql123", "SELECT * from " + table + " where " + key + " = '" + value + "'");
        Cursor c = db.rawQuery("SELECT WJMC from " + table + " where " + key + " = '" + value + "'", null);
        try {
            if (c.moveToNext()) {
                String string = c.getString(0);
                return string;
            } else
                return "";
        } catch (Exception e) {
            return "";
        }
    }

    //用于表之间的关联
    public String getName(String aim, String table, String key, String value) {
        Log.e("sql", "SELECT * from " + table + " where " + key + " = '" + value + "'");
        Cursor c = db.rawQuery("SELECT * from " + table + " where " + key + " = '" + value + "'", null);
        if (c.moveToNext()) {
            String string = c.getString(c.getColumnIndex(aim));
            c.close();
            return string;
        } else
            c.close();
        return "";
    }


    //用于缺陷登记 异常登记 类型查找
    public Cursor getCursor(String string) {
        Cursor cursor = db.rawQuery(string, null);
        return cursor;
    }

    //11.4

    //备用设备 Sbbj 根据设备id获取 12.16 zxj
    public List<SbbjModel> getSbbjById(String eq_id) {

        List<SbbjModel> list = new ArrayList<SbbjModel>();
        Cursor c = db.rawQuery("SELECT * from " + SBBJ_TABLE_NAME + " where eq_id ='" + eq_id + "'", null);
        while (c.moveToNext()) {
            SbbjModel model = new SbbjModel();
            model.eq_id = eq_id;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.spar_name = c.getString(c.getColumnIndex("spar_name"));
            model.spar_code_editable = c.getString(c.getColumnIndex("spar_code_editable"));
            model.model = c.getString(c.getColumnIndex("model"));
            model.tech_parameter_editable = c.getString(c.getColumnIndex("tech_parameter_editable"));
            model.producer = c.getString(c.getColumnIndex("producer"));
            model.unit = c.getString(c.getColumnIndex("unit"));
            model.price_editable = c.getString(c.getColumnIndex("price_editable"));
            model.quota_num_editable = c.getString(c.getColumnIndex("quota_num_editable"));
            model.stock_num = c.getString(c.getColumnIndex("stock_num"));
            model.storage_location_editable = c.getString(c.getColumnIndex("storage_location_editable"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //备用设备 Sbbj 根据层次编码获取 12.16 zxj
    public List<SbbjModel> getSbbjByCode(String layer_code) {
        List<SbbjModel> list = new ArrayList<SbbjModel>();
        Cursor c = db.rawQuery("SELECT * from " + SBBJ_TABLE_NAME + " where layer_code='" + layer_code + "'", null);
        while (c.moveToNext()) {
            SbbjModel model = new SbbjModel();
            model.layer_code = layer_code;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.spar_name = c.getString(c.getColumnIndex("spar_name"));
            model.spar_code_editable = c.getString(c.getColumnIndex("spar_code_editable"));
            model.model = c.getString(c.getColumnIndex("model"));
            model.tech_parameter_editable = c.getString(c.getColumnIndex("tech_parameter_editable"));
            model.producer = c.getString(c.getColumnIndex("producer"));
            model.unit = c.getString(c.getColumnIndex("unit"));
            model.price_editable = c.getString(c.getColumnIndex("price_editable"));
            model.quota_num_editable = c.getString(c.getColumnIndex("quota_num_editable"));
            model.stock_num = c.getString(c.getColumnIndex("stock_num"));
            model.storage_location_editable = c.getString(c.getColumnIndex("storage_location_editable"));
            list.add(model);
        }
        c.close();
        return list;
    }


    //设备资料 Sbzl 根据设备id获取 12.16 zxj
    public List<SbzlModel> getSbzlById(String eq_id) {

        List<SbzlModel> list = new ArrayList<SbzlModel>();
        Cursor c = db.rawQuery("SELECT A.* FROM TB_EQ_FILES A LEFT JOIN TB_FILE_EQ_REF R ON A.FILE_CODE = R.FILE_CODE " + " where R.EQ_ID  ='" + eq_id + "'" + " AND A.ZL_STATUS IN('0303601','0303602') " + " AND A.power_plant_id = '" + F1Application.getPowerPlantId() + "'", null);
        while (c.moveToNext()) {
            // Log.i("CCC",);
            SbzlModel model = new SbzlModel();
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.file_type_id = c.getString(c.getColumnIndex("file_type_id")) + "";
            model.file_name = c.getString(c.getColumnIndex("file_name"));
            model.file_format_editable = c.getString(c.getColumnIndex("file_format_editable"));
            model.file_version_editable = c.getString(c.getColumnIndex("file_version_editable"));
            model.file_status = c.getString(c.getColumnIndex("file_status"));
            model.fjid = c.getString(c.getColumnIndex("FJID"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            list.add(model);
            Log.i("SSS", list.toString() + "BBBBBBBBB");
        }
        c.close();
        return list;
    }

    //设备资料 Sbzl 根据层次编码获取 12.16 zxj
    public List<SbzlModel> getSbzlByCode(String layer_code) {

        List<SbzlModel> list = new ArrayList<SbzlModel>();
        Cursor c = db.rawQuery("SELECT * from " + SBZL_TABLE_NAME + " where layer_code='" + layer_code + "'", null);
        while (c.moveToNext()) {
            SbzlModel model = new SbzlModel();
            model.layer_code = layer_code;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.file_type_id = c.getString(c.getColumnIndex("file_type_id"));
            model.file_name = c.getString(c.getColumnIndex("file_name"));
            model.file_format_editable = c.getString(c.getColumnIndex("file_format_editable"));
            model.file_version_editable = c.getString(c.getColumnIndex("file_version_editable"));
            model.file_status = c.getString(c.getColumnIndex("file_status"));
            model.fjid = c.getString(c.getColumnIndex("FJID"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            list.add(model);
        }
        c.close();
        return list;
    }

    // 11.5 异常记录 根据设备id 12.16 zxj
    public List<YcjlModel> getYcjlById(String eq_id) {
        List<YcjlModel> list = new ArrayList<YcjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + YCGL_TABLE_NAME + " where eq_id='" + eq_id + "'", null);
        while (c.moveToNext()) {
            YcjlModel model = new YcjlModel();
            //缺一栏外键
            model.eq_id = eq_id;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));

            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.source_id = c.getString(c.getColumnIndex("source_id"));
            model.found_time = c.getString(c.getColumnIndex("found_time"));
            model.foundman_id = c.getString(c.getColumnIndex("foundman_id"));
            model.abn_describe = c.getString(c.getColumnIndex("abn_describe"));
            model.abn_status = c.getString(c.getColumnIndex("abn_status"));
            model.solved_time_editable = c.getString(c.getColumnIndex("solved_time_editable"));
            model.solvedman_id_editable = c.getString(c.getColumnIndex("solvedman_id_editable"));
            model.dealtype_id = c.getString(c.getColumnIndex("dealtype_id"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.inspect_id = c.getString(c.getColumnIndex("inspect_id"));
            model.patrol_id = c.getString(c.getColumnIndex("patrol_id"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //异常记录  根据层次编码获得 12.16 zxj
    public List<YcjlModel> getYcjlByCode(String layer_code) {
        List<YcjlModel> list = new ArrayList<YcjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + YCGL_TABLE_NAME + " where layer_code='" + layer_code + "'", null);
        while (c.moveToNext()) {
            YcjlModel model = new YcjlModel();
            //缺一栏外键
            model.layer_code = layer_code;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));

            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.source_id = c.getString(c.getColumnIndex("source_id"));
            model.found_time = c.getString(c.getColumnIndex("found_time"));
            model.foundman_id = c.getString(c.getColumnIndex("foundman_id"));
            model.abn_describe = c.getString(c.getColumnIndex("abn_describe"));
            model.abn_status = c.getString(c.getColumnIndex("abn_status"));
            model.solved_time_editable = c.getString(c.getColumnIndex("solved_time_editable"));
            model.solvedman_id_editable = c.getString(c.getColumnIndex("solvedman_id_editable"));
            model.dealtype_id = c.getString(c.getColumnIndex("dealtype_id"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.inspect_id = c.getString(c.getColumnIndex("inspect_id"));
            model.patrol_id = c.getString(c.getColumnIndex("patrol_id"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //缺陷记录 根据设备id获得 12.16 zxj
    public List<QxjlModel> getQxjlById(String eq_id) {
        List<QxjlModel> list = new ArrayList<QxjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + QXGL_TABLE_NAME + " where eq_id='" + eq_id + "'", null);
        while (c.moveToNext()) {
            QxjlModel model = new QxjlModel();
            //缺一栏外键
            model.eq_id = eq_id;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.inspect_guid = c.getString(c.getColumnIndex("inspect_id"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.source_id = c.getString(c.getColumnIndex("source_id"));
            model.found_time = c.getString(c.getColumnIndex("found_time"));
            model.foundman_id = c.getString(c.getColumnIndex("foundman_id"));
            model.level_id = c.getString(c.getColumnIndex("level_id"));
            model.type_id = c.getString(c.getColumnIndex("type_id"));
            model.defect_describe = c.getString(c.getColumnIndex("defect_describe"));
            model.defect_status = c.getString(c.getColumnIndex("defect_status"));
            model.solved_time_editable = c.getString(c.getColumnIndex("solved_time_editable"));
            model.solvedman_id_editable = c.getString(c.getColumnIndex("solvedman_id_editable"));
            model.solved_result = c.getString(c.getColumnIndex("solved_result"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.inspect_id = c.getString(c.getColumnIndex("inspect_id"));
            model.patrol_id = c.getString(c.getColumnIndex("patrol_id"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //缺陷记录 根据层次编码 12.16 zxj
    public List<QxjlModel> getQxjlByCode(String layer_code) {
        List<QxjlModel> list = new ArrayList<QxjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + QXGL_TABLE_NAME + " where layer_code='" + layer_code + "'", null);
        while (c.moveToNext()) {
            QxjlModel model = new QxjlModel();
            //缺一栏外键
            model.layer_code = layer_code;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.inspect_guid = c.getString(c.getColumnIndex("inspect_id"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.source_id = c.getString(c.getColumnIndex("source_id"));
            model.found_time = c.getString(c.getColumnIndex("found_time"));
            model.foundman_id = c.getString(c.getColumnIndex("foundman_id"));
            model.level_id = c.getString(c.getColumnIndex("level_id"));
            model.type_id = c.getString(c.getColumnIndex("type_id"));
            model.defect_describe = c.getString(c.getColumnIndex("defect_describe"));
            model.defect_status = c.getString(c.getColumnIndex("defect_status"));
            model.solved_time_editable = c.getString(c.getColumnIndex("solved_time_editable"));
            model.solvedman_id_editable = c.getString(c.getColumnIndex("solvedman_id_editable"));
            model.solved_result = c.getString(c.getColumnIndex("solved_result"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.inspect_id = c.getString(c.getColumnIndex("inspect_id"));
            model.patrol_id = c.getString(c.getColumnIndex("patrol_id"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //检修记录 根据设备id 12.16 zxj
    public List<JxjlModel> getJxjlById(String eq_id) {
        List<JxjlModel> list = new ArrayList<JxjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + JXJL_TABLE_NAME + " where eq_id='" + eq_id + "'", null);
        while (c.moveToNext()) {
            JxjlModel model = new JxjlModel();
            //缺一栏外键sql
            model.eq_id = eq_id;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.level_id = c.getString(c.getColumnIndex("level_id"));
            model.maintenance_time = c.getString(c.getColumnIndex("maintenance_time"));
            model.maintenance_describe = c.getString(c.getColumnIndex("maintenance_describe"));
            model.supervisor_id = c.getString(c.getColumnIndex("supervisor_id"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //检修记录 根据层次编码12.16 zxj
    public List<JxjlModel> getJxjlByCode(String layer_code) {
        List<JxjlModel> list = new ArrayList<JxjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + JXJL_TABLE_NAME + " where layer_code='" + layer_code + "'", null);
        while (c.moveToNext()) {
            JxjlModel model = new JxjlModel();
            //缺一栏外键sql
            model.layer_code = layer_code;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.level_id = c.getString(c.getColumnIndex("level_id"));
            model.maintenance_time = c.getString(c.getColumnIndex("maintenance_time"));
            model.maintenance_describe = c.getString(c.getColumnIndex("maintenance_describe"));
            model.supervisor_id = c.getString(c.getColumnIndex("supervisor_id"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //动作记录 根据设备id 12.16 zxj
    public List<DzjlModel> getDzjlById(String eq_id) {
        List<DzjlModel> list = new ArrayList<DzjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + DZJL_TABLE_NAME + " where eq_id='" + eq_id + "'", null);
        while (c.moveToNext()) {
            DzjlModel model = new DzjlModel();
            //缺一栏外键
            model.eq_id = eq_id;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.action_time = c.getString(c.getColumnIndex("action_time"));
            model.action_describe = c.getString(c.getColumnIndex("action_describe"));
            model.appraise = c.getString(c.getColumnIndex("appraise"));
            model.incorrect_reason = c.getString(c.getColumnIndex("incorrect_reason"));
            model.dept_name_editable = c.getString(c.getColumnIndex("dept_name_editable"));
            model.is_lubo_ok_editable = c.getString(c.getColumnIndex("is_lubo_ok_editable"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //动作记录 根据层次编码 12.16 zxj
    public List<DzjlModel> getDzjlByCode(String layer_code) {
        List<DzjlModel> list = new ArrayList<DzjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + DZJL_TABLE_NAME + " where layer_code='" + layer_code + "'", null);
        while (c.moveToNext()) {
            DzjlModel model = new DzjlModel();
            //缺一栏外键
            model.layer_code = layer_code;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.action_time = c.getString(c.getColumnIndex("action_time"));
            model.action_describe = c.getString(c.getColumnIndex("action_describe"));
            model.appraise = c.getString(c.getColumnIndex("appraise"));
            model.incorrect_reason = c.getString(c.getColumnIndex("incorrect_reason"));
            model.dept_name_editable = c.getString(c.getColumnIndex("dept_name_editable"));
            model.is_lubo_ok_editable = c.getString(c.getColumnIndex("is_lubo_ok_editable"));
            list.add(model);
        }
        c.close();
        return list;
    }
    //投退记录 根据设备id 12.16 zxj

    public List<TtjlModel> getTtjlById(String eq_id) {
        List<TtjlModel> list = new ArrayList<TtjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + TTJL_TABLE_NAME + " where eq_id='" + eq_id + "'", null);
        String sql = "SELECT * from " + TTJL_TABLE_NAME + " where eq_id='" + eq_id + "'";
        Log.e("abcdefg", sql);
        while (c.moveToNext()) {
            TtjlModel model = new TtjlModel();
            //缺一栏外键
            model.eq_id = eq_id;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.exit_time = c.getString(c.getColumnIndex("exit_time"));
            model.startup_time = c.getString(c.getColumnIndex("startup_time"));
            model.exit_function = c.getString(c.getColumnIndex("exit_function"));
            model.exit_describe = c.getString(c.getColumnIndex("exit_describe"));
            model.is_fault_exit_editable = c.getString(c.getColumnIndex("is_fault_exit_editable"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //投退记录 根据层次编码 12.16 zxj

    public List<TtjlModel> getTtjlByCode(String layer_code) {
        List<TtjlModel> list = new ArrayList<TtjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + TTJL_TABLE_NAME + " where layer_code='" + layer_code + "'", null);
        while (c.moveToNext()) {
            TtjlModel model = new TtjlModel();
            //缺一栏外键
            model.layer_code = layer_code;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attacheq_id = c.getString(c.getColumnIndex("attacheq_id"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.exit_time = c.getString(c.getColumnIndex("exit_time"));
            model.startup_time = c.getString(c.getColumnIndex("startup_time"));
            model.exit_function = c.getString(c.getColumnIndex("exit_function"));
            model.exit_describe = c.getString(c.getColumnIndex("exit_describe"));
            model.is_fault_exit_editable = c.getString(c.getColumnIndex("is_fault_exit_editable"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //异动记录 根据设备id 12.16 zxj
    public List<YdjlModel> getYdjlById(String eq_id) {
        List<YdjlModel> list = new ArrayList<YdjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + YDJL_TABLE_NAME + " where eq_id='" + eq_id + "'", null);
        while (c.moveToNext()) {
            YdjlModel model = new YdjlModel();
            //缺一栏外键
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.eq_id = eq_id;
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.yidong_time = c.getString(c.getColumnIndex("yidong_time"));
            model.yidong_reason = c.getString(c.getColumnIndex("yidong_reason"));
            model.yidong_describe = c.getString(c.getColumnIndex("yidong_describe"));
            model.supervisor_id = c.getString(c.getColumnIndex("supervisor_id"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //异动记录 根据层次编码12.16 zxj
    public List<YdjlModel> getYdjlByCode(String layer_code) {
        List<YdjlModel> list = new ArrayList<YdjlModel>();
        //需修改外键
        Cursor c = db.rawQuery("SELECT * from " + YDJL_TABLE_NAME + " where layer_code='" + layer_code + "'", null);
        while (c.moveToNext()) {
            YdjlModel model = new YdjlModel();
            //缺一栏外键
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.layer_code = layer_code;
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.yidong_time = c.getString(c.getColumnIndex("yidong_time"));
            model.yidong_reason = c.getString(c.getColumnIndex("yidong_reason"));
            model.yidong_describe = c.getString(c.getColumnIndex("yidong_describe"));
            model.supervisor_id = c.getString(c.getColumnIndex("supervisor_id"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //基本信息表  根据系统编号
    public LedgerModel getLedgerbyId(String guid) {
        LedgerModel model = new LedgerModel();
        Cursor c = db.rawQuery("SELECT * from " + JIBENXINXI_NAME + " where GUID='" + guid + "'", null);
        if (c.moveToNext()) {
            model.guid = guid;
            //层次编码
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));

            //专业id
            model.eq_edetp_id = c.getString(c.getColumnIndex("eq_edetp_id"));
            //设备名称
            model.eq_name = c.getString(c.getColumnIndex("eq_name"));
            //设备类型id
            model.eq_type_id = c.getString(c.getColumnIndex("eq_type_id"));
            //设备型号id
            model.eq_model_id = c.getString(c.getColumnIndex("eq_model_id"));
            //生产厂家id
            model.eq_producer_id = c.getString(c.getColumnIndex("eq_producer_id"));
            //安装位置id
            model.eq_postion_id = c.getString(c.getColumnIndex("eq_postion_id"));

            //导入设备类型
            model.eq_imp_type = c.getString(c.getColumnIndex("eq_imp_type"));
            //导入设备型号
            model.eq_imp_model = c.getString(c.getColumnIndex("eq_imp_model"));
            //导入生产厂家
            model.ep_imp_producer = c.getString(c.getColumnIndex("ep_imp_producer"));
            //导入安装位置
            model.eq_imp_postion = c.getString(c.getColumnIndex("eq_imp_postion"));
            //投运日期
            model.eq_operation_date = c.getString(c.getColumnIndex("eq_operation_date"));
            //状态
            model.eq_status = c.getString(c.getColumnIndex("eq_status"));
            //责任人id
            model.eq_supervisor_id = c.getString(c.getColumnIndex("eq_supervisor_id"));
//            model.eq_supervisor_id = c.getString(c.getColumnIndex("TYRQ"));
            //设备等级
            model.eq_level = c.getString(c.getColumnIndex("eq_level"));
//            model.eq_level = getName("code_name", "tb_sys_code", "code", c.getString(c.getColumnIndex("sjsm")));
//model.jyzq = c.getString(c.getColumnIndex("JYZQ"));
            //设备编码
            model.eq_code = c.getString(c.getColumnIndex("eq_code"));
            //更新人id
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            //更新时间
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            //技术参数
            model.eq_tech_parameter = c.getString(c.getColumnIndex("eq_tech_parameter"));
            //备注
            model.note = c.getString(c.getColumnIndex("note"));
            //发电厂id
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            //创建人id
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            //创建时间
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));


            return model;
        }
        c.close();
        return null;
    }

    /**
     * 根据设备id 返回其附属设备列表 12.16 zxj
     */
    public List<FssbModel> getFssbById(String eq_id) {
        List<FssbModel> list = new ArrayList<FssbModel>();
        Cursor c = db.rawQuery("SELECT * from " + FSSB_TABLE_NAME + " where eq_id='" + eq_id + "'", null);
        while (c.moveToNext()) {
            FssbModel model = new FssbModel();
            model.eq_id = eq_id;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.layer_code = c.getString(c.getColumnIndex("layer_code"));
            model.attacheq_name = c.getString(c.getColumnIndex("attacheq_name"));
            model.code_editable = c.getString(c.getColumnIndex("code_editable"));
            model.model = c.getString(c.getColumnIndex("model"));
            model.tech_parameter_editable = c.getString(c.getColumnIndex("tech_parameter_editable"));
            model.producer_editable = c.getString(c.getColumnIndex("producer_editable"));
//            model.sctp = c.getString(c.getColumnIndex("SCTP"));

            model.num_editable = c.getString(c.getColumnIndex("num_editable"));
            model.operation_date_editable = c.getString(c.getColumnIndex("operation_date_editable"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            list.add(model);
        }
        c.close();
        return list;
    }

    /**
     * 返回其附属设备列表  根据层次编码  12.16 zxj
     */
    public List<FssbModel> getFssbByCode(String layer_code) {
        List<FssbModel> list = new ArrayList<FssbModel>();
        Cursor c = db.rawQuery("SELECT * from " + FSSB_TABLE_NAME + " where layer_code='" + layer_code + "'", null);
        while (c.moveToNext()) {
            FssbModel model = new FssbModel();
            model.layer_code = layer_code;
            model.guid = c.getString(c.getColumnIndex("guid"));
            model.eq_id = c.getString(c.getColumnIndex("eq_id"));
            model.attacheq_name = c.getString(c.getColumnIndex("attacheq_name"));
            model.code_editable = c.getString(c.getColumnIndex("code_editable"));
            model.model = c.getString(c.getColumnIndex("model"));
            model.tech_parameter_editable = c.getString(c.getColumnIndex("tech_parameter_editable"));
            model.producer_editable = c.getString(c.getColumnIndex("producer_editable"));
//            model.sctp = c.getString(c.getColumnIndex("SCTP"));

            model.num_editable = c.getString(c.getColumnIndex("num_editable"));
            model.operation_date_editable = c.getString(c.getColumnIndex("operation_date_editable"));
            model.attachment = c.getString(c.getColumnIndex("attachment"));
            model.attachment_path = c.getString(c.getColumnIndex("attachment_path"));
            model.note = c.getString(c.getColumnIndex("note"));
            model.power_plant_id = c.getString(c.getColumnIndex("power_plant_id"));
            model.creat_user_id = c.getString(c.getColumnIndex("creat_user_id"));
            model.creat_time = c.getString(c.getColumnIndex("creat_time"));
            model.update_user_id = c.getString(c.getColumnIndex("update_user_id"));
            model.update_time = c.getString(c.getColumnIndex("update_time"));
            list.add(model);
        }
        c.close();
        return list;
    }

    //根据layer_code 返回需要展示的表的集合
    public List<String> getListByLayerCode(String layer_code) {
        List<String> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from TB_EQ_LAYER_FORM where layer_code = '" + layer_code + "'", null);

        while (cursor.moveToNext()) {
            String string = cursor.getString(cursor.getColumnIndex("form_code"));
            list.add(string);
        }
        cursor.close();
        return list;
    }

    //根据专业id 返回需要展示的表的集合
    public List<String> getListByZYid(String eq_dept_id) {
        List<String> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from TB_EQ_DEPT_FORM where eq_dept_id ='" + eq_dept_id + "'", null);
        while (cursor.moveToNext()) {
            String string = cursor.getString(cursor.getColumnIndex("form_code"));
            list.add(string);
        }
        cursor.close();
        return list;
    }

    public List<SbcjModel> searchDev(String key) {
        List<SbcjModel> list = new ArrayList<>();

        String sql = "select * from TB_DEV_BASICINFO where eq_name LIKE '%" + key + "%'";
        Log.i("MainActivity", sql);
        Cursor c = db.rawQuery(sql, null);
        while (c.moveToNext()) {
            SbcjModel model = new SbcjModel();
            model.id = c.getString(c.getColumnIndex("GUID"));
            model.h1Name = c.getString(c.getColumnIndex("SBMC"));
            model.h2Name = getName("CJMC", "TB_DEV_HD_SBCC", "GUID", c.getString(c.getColumnIndex("XT")));
            model.h3Name = getName("CJMC", "TB_DEV_HD_SBCC", "GUID", c.getString(c.getColumnIndex("JZ")));
            list.add(model);
        }
        c.close();
        return list;
    }

    /**
     * 根据登录用户名获取该用户姓名
     *
     * @param id
     * @return
     */
    public String[] getPersonNameById(String id) {
        String[] ss = new String[2];
        Cursor c = db.rawQuery("SELECT GUID,PERS_NAME from " + PERSON_TABLE_NAME + " where LOGO_ID='" + id + "'", null);
        if (c.moveToNext()) {
            ss[0] = c.getString(c.getColumnIndex("GUID"));
            ss[1] = c.getString(c.getColumnIndex("PERS_NAME"));
            Log.i(TAG, "ss=" + ss[0] + "   " + ss[1]);
        }
        c.close();
        return ss;
    }

    /**
     * 获取数据库所有的文件
     *
     * @return
     */
    public List<FileLbModel> getAllFiles() {
        List<FileLbModel> list = new ArrayList<>();
        Cursor c = db.rawQuery("select * from TB_FILE_LB", null);
        while (c.moveToNext()) {
            FileLbModel model = new FileLbModel();
            model.guid = c.getString(c.getColumnIndex("GUID"));
            model.obj_id = c.getString(c.getColumnIndex("OBJ_ID"));
            model.wjmc = c.getString(c.getColumnIndex("WJMC"));
            model.lx = c.getString(c.getColumnIndex("LX"));
            list.add(model);
        }
        c.close();
        return list;
    }


    //根据层次编码获取基本信息表集合
    public List<LedgerModel> getLedgerModelByLayerCode(String ss) {
        List<LedgerModel> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from TB_EQ_BASICINFO where layer_code ='" + ss + "'", null);
        while (cursor.moveToNext()) {
            LedgerModel model = new LedgerModel();
            model.guid = cursor.getString(cursor.getColumnIndex("guid"));
            model.layer_code = ss;
            model.eq_edetp_id = cursor.getString(cursor.getColumnIndex("eq_edetp_id"));
            model.eq_name = cursor.getString(cursor.getColumnIndex("eq_name"));
            model.eq_type_id = cursor.getString(cursor.getColumnIndex("eq_type_id"));
            model.eq_model_id = cursor.getString(cursor.getColumnIndex("eq_model_id"));
            model.eq_producer_id = cursor.getString(cursor.getColumnIndex("eq_producer_id"));
            model.eq_postion_id = cursor.getString(cursor.getColumnIndex("eq_postion_id"));
            model.eq_imp_type = cursor.getString(cursor.getColumnIndex("eq_imp_type"));
            model.eq_imp_model = cursor.getString(cursor.getColumnIndex("eq_imp_model"));
            model.ep_imp_producer = cursor.getString(cursor.getColumnIndex("ep_imp_producer"));
            model.eq_imp_postion = cursor.getString(cursor.getColumnIndex("eq_imp_postion"));
            model.eq_operation_date = cursor.getString(cursor.getColumnIndex("eq_operation_date"));
            model.eq_status = cursor.getString(cursor.getColumnIndex("eq_status"));
            model.eq_supervisor_id = cursor.getString(cursor.getColumnIndex("eq_supervisor_id"));
            model.eq_level = cursor.getString(cursor.getColumnIndex("eq_level"));
            model.eq_code = cursor.getString(cursor.getColumnIndex("eq_code"));
            model.update_user_id = cursor.getString(cursor.getColumnIndex("update_user_id"));
            model.update_time = cursor.getString(cursor.getColumnIndex("update_time"));
            model.eq_tech_parameter = cursor.getString(cursor.getColumnIndex("eq_tech_parameter"));
            model.note = cursor.getString(cursor.getColumnIndex("note"));
            model.power_plant_id = cursor.getString(cursor.getColumnIndex("power_plant_id"));
            model.creat_user_id = cursor.getString(cursor.getColumnIndex("creat_user_id"));
            model.creat_time = cursor.getString(cursor.getColumnIndex("creat_time"));
            list.add(model);
        }
        cursor.close();
        return list;

    }

    //根据parent_id 拿到 设备树model
    public List<SbsModel> getSbsModelByparentid(String s) {
        List<SbsModel> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from TB_EQ_NAV where parent_id = '" + s + "'", null);
        while (cursor.moveToNext()) {
            SbsModel model = new SbsModel();
            model.guid = cursor.getString(cursor.getColumnIndex("guid"));
            model.layer_name = cursor.getString(cursor.getColumnIndex("layer_name"));
            model.layer_code = cursor.getString(cursor.getColumnIndex("layer_code"));
            model.parent_id = cursor.getString(cursor.getColumnIndex("parent_id"));
            model.layer_type = cursor.getString(cursor.getColumnIndex("layer_type"));
            model.link_dept_id = cursor.getString(cursor.getColumnIndex("link_dept_id"));
            model.config_show_content = cursor.getString(cursor.getColumnIndex("config_show_content"));
            model.sort = cursor.getString(cursor.getColumnIndex("sort"));
            model.note = cursor.getString(cursor.getColumnIndex("note"));
            model.power_plant_id = cursor.getString(cursor.getColumnIndex("power_plant_id"));
            model.creat_user_id = cursor.getString(cursor.getColumnIndex("creat_user_id"));
            model.creat_time = cursor.getString(cursor.getColumnIndex("creat_time"));
            model.update_user_id = cursor.getString(cursor.getColumnIndex("update_user_id"));
            model.update_time = cursor.getString(cursor.getColumnIndex("update_time"));
            list.add(model);
        }
        cursor.close();
        return list;
    }

    //根据表单编码和适用专业得到设备表单自定义字段的模型
    public List<SbbdzdyModel> getSbbdzdyModel(String form_code, String id_zhuanye) {
        List<SbbdzdyModel> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from TB_EQ_FORM_CUSTOMFIELDS where form_code = '" + form_code + "' and eq_detp_id = '" + id_zhuanye + "'", null);
        String sql = "select * from TB_EQ_FORM_CUSTOMFIELDS where form_code = '" + form_code + "' and eq_detp_id = '" + id_zhuanye + "'";
        Log.e("1231", sql);
        while (cursor.moveToNext()) {
            Log.e("1231", "没有游标");
            SbbdzdyModel model = new SbbdzdyModel();
            model.guid = cursor.getString(cursor.getColumnIndex("guid"));
            model.form_code = cursor.getString(cursor.getColumnIndex("form_code"));
            model.cus_name = cursor.getString(cursor.getColumnIndex("cus_name"));
            model.field_format = cursor.getString(cursor.getColumnIndex("field_format"));
            model.possible_values = cursor.getString(cursor.getColumnIndex("possible_values"));
            model.min_length = cursor.getString(cursor.getColumnIndex("min_length"));
            model.max_length = cursor.getString(cursor.getColumnIndex("max_length"));
            model.is_required = cursor.getString(cursor.getColumnIndex("is_required"));
            model.eq_detp_id = cursor.getString(cursor.getColumnIndex("eq_detp_id"));
            model.is_filter = cursor.getString(cursor.getColumnIndex("is_filter"));
            model.searchable = cursor.getString(cursor.getColumnIndex("searchable"));
            model.default_value = cursor.getString(cursor.getColumnIndex("default_value"));
            model.editable = cursor.getString(cursor.getColumnIndex("editable"));
            model.is_show_eqbook = cursor.getString(cursor.getColumnIndex("is_show_eqbook"));
            model.description = cursor.getString(cursor.getColumnIndex("description"));
            model.order_num = cursor.getString(cursor.getColumnIndex("order_num"));
            model.note = cursor.getString(cursor.getColumnIndex("note"));
            model.power_plant_id = cursor.getString(cursor.getColumnIndex("power_plant_id"));
            model.creat_user_id = cursor.getString(cursor.getColumnIndex("creat_user_id"));
            model.creat_time = cursor.getString(cursor.getColumnIndex("creat_time"));
            model.update_user_id = cursor.getString(cursor.getColumnIndex("update_user_id"));
            model.update_time = cursor.getString(cursor.getColumnIndex("update_time"));
            Log.e("1231", "执行了");
            list.add(model);
        }
        cursor.close();
        return list;
    }

    //根据自定义字段id和所属数据行guid得到自定义字段的值
    public String getZdyzdValue(String zdy_id, String guid) {
        String string = "";
        Cursor cursor = db.rawQuery("select * from TB_EQ_FORM_CUSTOMFIELDS_VALUE where customdfield_id = '" + zdy_id + "' and belongrow_guid = '" + guid + "'", null);
        while (cursor.moveToNext()) {
            string = cursor.getString(cursor.getColumnIndex("value"));
        }
        cursor.close();
        return string;
    }

    //根据表单编码和字段名称得到是否可见
    public String getVisible(String form_code, String field_name) {
        String string1 = "";
        Cursor cursor1 = db.rawQuery("select * from TB_EQ_NAV where layer_type = '0500301'", null);
        while (cursor1.moveToNext()) {
            string1 = cursor1.getString(cursor1.getColumnIndex("link_dept_id"));
        }
        String string = "";
        Cursor cursor = db.rawQuery("select * from TB_EQ_FORM_STDFIELDS where form_code = '" + form_code + "' and field_code = '" + field_name + "' and power_plant_id = '" + string1 + "'", null);
        String sql = "select * from TB_EQ_FORM_STDFIELDS where form_code = '" + form_code + "' and field_code = '" + field_name + "'";
        Log.e("1231", sql);
        while (cursor.moveToNext()) {
            string = cursor.getString(cursor.getColumnIndex("visible"));
        }
        cursor.close();
        return string;
    }

    public String getPowerPlantId(String userId) {
        String sql = "SELECT D.GLFW_DWID, (SELECT D2.DEPT_NAME FROM TB_SYS_DEPARTMENT D2 WHERE D2.DEPT_ID = D.GLFW_DWID) FROM TB_SYS_DEPARTMENT D INNER JOIN tb_sys_person p ON D.DEPT_ID = P.DEPT_ID WHERE P.PERS_ID = '" + userId + "'";
        Cursor c = db.rawQuery(sql, null);
        String id = "";
        if (c.moveToFirst())
            id = c.getString(0);
        c.close();
        return id;
    }

    public String getName2(String aim, String table, String key, String value) {
        Log.e("sqla", "SELECT * from " + table + " where " + key + " = '" + value + "'");
        Cursor c = db.rawQuery("SELECT * from " + table + " where " + key + " = '" + value + "' and LABEL_CODE = '05004'", null);
        if (c.moveToNext()) {
            String string = c.getString(c.getColumnIndex(aim));
            c.close();
            return string;
        } else
            c.close();
        return "";
        //    try {

//        } catch (IllegalStateException e) {
//            return "";
//        }
    }

    /**
     * 获取数据库所有的文档列表
     *
     * @return
     */
    public List<DocModel> getAllDocs() {
        List<DocModel> list = new ArrayList<>();

        Cursor c = db.rawQuery("select * from TB_BZ_BZGL", null);
        while (c.moveToNext()) {

            DocModel model = new DocModel();
            model.setGuid(c.getString(c.getColumnIndex("GUID")));
            model.setDocName(getName("WJMC", "TB_FILE_LB", "GUID", c.getString(c.getColumnIndex("FJID"))));
            model.setDocType(getName("TYPE", "TB_BZ_BZGLS", "GUID", c.getString(c.getColumnIndex("TYPE"))));
            model.setDate(c.getString(c.getColumnIndex("UPDATE_TIME")));
            model.setCreatPerson(c.getString(c.getColumnIndex("UPLOADOR")));
            // Log.i("ABC",list.toString());
            list.add(model);
        }
        c.close();
        return list;
    }

    /**
     * 由文件类型获取数据库文档列表
     *
     * @return
     */
    public List<DocModel> getAllDocsById(String guid) {
        List<DocModel> list = new ArrayList<>();

        Cursor c = db.rawQuery("select * from TB_BZ_BZGL" + " where type = '" + guid+ "'", null);
        while (c.moveToNext()) {
            DocModel model = new DocModel();
            model.setGuid(c.getString(c.getColumnIndex("GUID")));
            model.setDocName(getName("WJMC", "TB_FILE_LB", "GUID", c.getString(c.getColumnIndex("FJID"))));
            model.setDocType(guid);
            model.setDate(c.getString(c.getColumnIndex("UPDATE_TIME")));
            model.setCreatPerson(c.getString(c.getColumnIndex("UPLOADOR")));
            // Log.i("ABC",list.toString());
            list.add(model);
        }
        c.close();
        return list;
    }

    /**
     * 获取指定pid的文档类型
     *
     * @return
     */
    public List<DocH2Model> getAllDocsTypeByPid2(String pid) {
        List<DocH2Model> list = new ArrayList<>();
        Cursor c = db.rawQuery("select * from TB_BZ_BZGLS" + " where pid = '" + pid +"'", null);
        while (c.moveToNext()) {
            DocH2Model model = new DocH2Model();
            model.guid = c.getString(c.getColumnIndex("GUID"));
            model.type = c.getString(c.getColumnIndex("TYPE"));
            model.pid = c.getString(c.getColumnIndex("PID"));
            model.update_time = c.getString(c.getColumnIndex("UPDATE_TIME"));
            list.add(model);
        }
        c.close();
        return list;
    }

    /**
     * 获取数据库文档头
     *
     * @return
     */
    public List<DocH1Model> getAllDocsTypeByPid() {
        List<DocH1Model> list = new ArrayList<>();
        Cursor c = db.rawQuery("select * from TB_BZ_BZGLS " + " where guid = '00001' "  , null);
        while (c.moveToNext()) {
            DocH1Model model = new DocH1Model();
            model.guid = c.getString(c.getColumnIndex("GUID"));
            model.type = c.getString(c.getColumnIndex("TYPE"));
            model.pid = c.getString(c.getColumnIndex("PID"));
            model.update_time = c.getString(c.getColumnIndex("UPDATE_TIME"));
            list.add(model);
        }
        c.close();
        return list;
    }

    /**
     * 删除3天前的历史点检数据
     *
     * @return
     */
    public boolean deleteHisCheckRes() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        Date severalDaysAgo = new Date(curDate.getTime() - 2 * 24 * 60 * 60 * 1000);
        String several = "'" + formatter.format(severalDaysAgo) + " 00:00:00'";
        int result = db.delete(TASK_RESULT_TABLE_NAME, "creat_time <=?", new String[]{several});
        return result == -1 ? false : true;
    }


}